<!--

/*
** Copyright (c) 2016 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Transform Feedback Conformance Tests</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">#version 300 es
in vec4 in_data;
uniform int flag;
out vec4 out_data;
void main(void) {
    if (flag > 0) {
        out_data = in_data + vec4(2.0, 3.0, 4.0, 5.0);
    }
}
</script>
<script id="fshader" type="x-shader/x-fragment">#version 300 es
precision mediump float;
out vec4 out_color;
void main(void) {
    out_color = vec4(1.0, 1.0, 1.0, 1.0);
}
</script>
<script>
"use strict";
description("This test verifies if an output variable is specified to be streamed to a transform feedback buffer but not actually written, the value defaults to 0.");

debug("");

var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas, null, 2);

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");

    debug("");
    debug("Testing transform feedback works fine");
    runTest(1);

    debug("");
    debug("Testing unwritten output variables default to zero");
    runTest(0);
}

function runTest(flag) {
    var in_data = [
        1.0, 2.0, 3.0, 4.0,
        2.0, 4.0, 8.0, 16.0,
        0.75, 0.5, 0.25, 0.0
    ];

    var in_buffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, in_buffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(in_data), gl.STATIC_DRAW);

    var out_buffer = gl.createBuffer();
    gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, out_buffer);
    gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, Float32Array.BYTES_PER_ELEMENT * in_data.length, gl.STATIC_DRAW);

    // Create the transform feedback program
    var program = wtu.setupTransformFeedbackProgram(gl, ["vshader", "fshader"],
        ["out_data"], gl.SEPARATE_ATTRIBS,
        ["in_data"]);
    var loc = gl.getUniformLocation(program, "flag");
    if (!program || !loc) {
        testFailed("Fail to set up the program");
        return;
    }
    gl.uniform1i(loc, flag);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Set up program should succeed");

    // Draw the the transform feedback buffers
    var tf = gl.createTransformFeedback();

    gl.enableVertexAttribArray(0);
    gl.bindBuffer(gl.ARRAY_BUFFER, in_buffer);
    gl.vertexAttribPointer(0, 4, gl.FLOAT, false, 16, 0);

    gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, out_buffer);

    gl.enable(gl.RASTERIZER_DISCARD);
    gl.beginTransformFeedback(gl.POINTS);

    gl.drawArrays(gl.POINTS, 0, 3);

    gl.endTransformFeedback();
    gl.disable(gl.RASTERIZER_DISCARD);

    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, null);

    // Verify the output buffer contents
    var expected_data;
    if (flag > 0) {
        expected_data = [
            3.0, 5.0, 7.0, 9.0,
            4.0, 7.0, 12.0, 21.0,
            2.75, 3.5, 4.25, 5.0
        ];
    } else {
        expected_data = [
            0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0
        ];
    }
    gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, out_buffer);
    wtu.checkFloatBuffer(gl, gl.TRANSFORM_FEEDBACK_BUFFER, expected_data);
}

debug("");
var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>
